<?php
	// Work-around for setting up a session because Flash Player doesn't send the cookies
	if (isset($_POST["PHPSESSID"])) {
		session_id($_POST["PHPSESSID"]);
	}
	session_start();

	$tovar_img_dir = "/user/tovar/";
	$tovar_tmb_dir = "/user/images/tovar_tmb/";
	include_once ('../core/class.upload.php');

	include ("../user/counter.php");

	if (empty($tovar_ids_string)) {		$new_id = 1;
		$cur_counter = array($new_id);
	} else {		$cur_counter = explode(",",$tovar_ids_string);
		$last_id = array_pop($cur_counter);
		$new_id = $last_id*1 + 1;
		array_push($cur_counter,$last_id,$new_id);
	}
	$cur_counter_string = implode(",",$cur_counter);
	file_put_contents("../user/counter.php",'<? $tovar_ids_string="'.$cur_counter_string.'"; ?>');

if (!empty($_FILES['Filedata']['name'])) {
				if ($_FILES['Filedata']['error'] === 0) {
					$in_ext = substr($_FILES['Filedata']['name'],strpos($_FILES['Filedata']['name'],'.'));
					copy($_FILES['Filedata']['tmp_name'],'..'.$tovar_img_dir.'t'.$new_id.strtolower($in_ext));
					$handle = new upload($_FILES['Filedata']);
					if ($handle->uploaded) {
						$handle->file_new_name_body  	= 't'.$new_id;
						$handle->image_resize         	= true;
						$handle->image_x              	= 400;
						$handle->image_y        		= 345;
						$handle->image_ratio			= true;
						$handle->file_overwrite			= true;
						$handle->auto_create_dir		= true;
						$handle->process('..'.$tovar_tmb_dir);
						if ($handle->processed) {
							$handle->clean();
						} else {
							echo 'error : ' . $handle->error;
						}
					}
				}
			}

	$cur_tovar_list = file_get_contents("../user/tovar.php");
	$cur_tovar_list = substr($cur_tovar_list,0,-2);
	$cur_tovar_list .= '
	$tovar_'.$new_id.'_name = "название";
	$tovar_'.$new_id.'_desc = "";
	$tovar_'.$new_id.'_category = "'.$_POST['category_name'].'";
	$tovar_'.$new_id.'_show_old = "0";
	$tovar_'.$new_id.'_old = "";
	$tovar_'.$new_id.'_new = "";
	$tovar_'.$new_id.'_std_price_name = "1";
	$tovar_'.$new_id.'_img = "t'.$new_id.'.'.$handle->file_dst_name_ext.'";
	$tovar_'.$new_id.'_cat_pos = "'.$new_id.'";
	$tovar_'.$new_id.'_g_pos = "10'.$new_id.'";

?>';
	file_put_contents("../user/tovar.php",$cur_tovar_list);


/*	$fp = fopen ("log.txt", "a+");
 fwrite ($fp, var_export($_POST,true)."\n");
 fwrite ($fp, "-=".$last_id."=-\n");
 //fwrite ($fp, "-=-".$cur_tovar_list."-=-\n");
 fclose ($fp);      */
	exit(0);
?>